angular.module('starter.controllers', [])
// 定义控制器
.controller('homeCtrl', function() {
  $('.user_list li').ljqSlide({
    speed: 500,
    delay: 2000
  })
  $('.scroll-content').scroll(function () {
      if($('#chart').offset().top <= 90){
        $('.div_chart li div').addClass('animation')
        $('#less').addClass('animation2')
        $('.runchart').addClass('animation')
      }
  })
  $('div.search input').blur(function(){
      $('div.search').addClass('hide')
  })

})

.controller('discoverCtrl', function($scope,$http,Chats) {
  $http({
    method:'POST',
    url:'http://127.0.0.1:3000/discover'
  }).success(function(data){
    if(data.code == 'success'){
      $scope.list1 = data.data.slice(0,4)
      $scope.list2 = data.data.slice(4,8)
      $scope.lists = data.data.slice(8)
    }else{
      console.log('渲染失败')
    }
  });

    //$('ul.chats_ul a').click(function(){
    //  console.log('click')
    //  $scope.id = this.href.replace('http://127.0.0.1:3000/#/tab/discover/chat-detail/','')
    //  console.log($scope.id)
    //  $http({
    //    method:'POST',
    //    url:('/discover/chat-detail/' + $scope.id)
    //  }).success(function(data){
    //    console.log(data.data)
    //    if(data.code == 'success'){
    //      $scope.chat = data.data
    //    }else{
    //      console.log('渲染失败')
    //    }
    //  })
    //})


    $scope.imgs = [
      {
        id:'0',
        src:'https://staticssl.keepcdn.com/2017/02/23/14/1487829913793_750x340.jpg'
      },
      {
        id:'1',
        src:'https://staticssl.keepcdn.com/2017/02/16/11/1487215752360_750x340.jpg'
      },
      {
        id:'2',
        src:'https://staticssl.keepcdn.com/2017/02/17/10/1487299197136_750x340.jpg'
      }
    ];
    $scope.chats = Chats.all()

//

})
  .controller('chatDetailCtrl', function($scope, $stateParams, Chats) {

  })
.controller('courseCtrl', function($scope, $stateParams, Chats) {

})
.controller('loginCtrl', function($scope,$http,$rootScope) {
    $scope.formData = {}
    $scope.postForm = function(){
      $http({
        method:'POST',
        url:'http://127.0.0.1:3000/login',
        data: $.param($scope.formData),
        headers:{'Content-Type':"application/x-www-form-urlencoded"}
      }).success(function(data){
        console.log(data)
        if(data.code == 'success'){
          location.href = 'http://192.168.191.1:8100/#/tab/me'
        }else{
          location.href = 'http://192.168.191.1:8100/#/tab/login'
        }
      })
    }
  })
.controller('meCtrl', function($scope,$http) {
  console.log('meCtrl')
  $scope.user = $.cookie('user')
  console.log($scope.user)
  $scope.logout = function(){
    $http({
      method:'POST',
      url:'http://127.0.0.1:3000/logout'
    }).success(function(data){
      if(data.code == 'success'){
        location.href = 'http://192.168.191.1:8100/#/tab/login'
      }
    })
  }
})

.controller('regCtrl', function($scope,$http) {
    $scope.formData = {}
    $scope.postForm = function(){
      $http({
        method:'POST',
        url:'http://127.0.0.1:3000/reg',
        data: $.param($scope.formData),
        headers:{'Content-Type':"application/x-www-form-urlencoded"}
      }).success(function(data){
        console.log(data)
        if(data.code == 'success'){
            location.href = 'http://192.168.191.1:8100/#/tab/login'
        }else{
          //$scope.message = data.message;
          setTimeout(function(){
            location.href = 'http://192.168.191.1:8100/#/tab/reg'
          },1500)
        }
      })
    }
  });

